package com.wxmit.module.system.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wxmit.base.util.StringUtils;
import com.wxmit.framework.satoken.core.util.StpUserUtil;
import com.wxmit.module.system.domain.SysColumnSetting;
import com.wxmit.module.system.mapper.SysColumnSettingMapper;
import com.wxmit.system.base.core.ShushanServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * SysColumnSettingService
 *
 * @author huangxiong
 * @version V1.0
 * @date 2022/12/5 16:18
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SysColumnSettingService extends ShushanServiceImpl<SysColumnSettingMapper, SysColumnSetting> {


    private final SysColumnSettingMapper baseMapper;
    /**
     *  根据路径查找 列设置
     * @param path 路径
     * @return 列设置，无时候返回null
     */
    public List<SysColumnSetting> getSettingByPath(String path){

        LambdaQueryChainWrapper<SysColumnSetting> lambdaQuery = lambdaQuery();
        lambdaQuery.eq(SysColumnSetting::getUserId, StpUserUtil.getUserId());
        lambdaQuery.eq(SysColumnSetting::getPath,path);
        lambdaQuery.orderByAsc(SysColumnSetting::getSort);
        List<SysColumnSetting> sysColumnSettings = lambdaQuery.list();
        return sysColumnSettings;
    }


    /**
     *  添加或更新列设置
     * @param settings 列设置对象
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void addOrUpdateBatch(List<SysColumnSetting> settings){
        Assert.isTrue(!CollectionUtil.isEmpty(settings),"列设置不能为空！");
        final Long userId = StpUserUtil.getUserId();
            List<SysColumnSetting> addList = new ArrayList<>();
            List<SysColumnSetting> updateList = new ArrayList<>();
            settings.stream().filter(it-> StringUtils.isNotEmpty(it.getName()))
                    .forEach(it->{
                        it.setUserId(userId);
                        if(Objects.isNull(it.getId())){
                            addList.add(it);
                        }else{
                            updateList.add(it);
                        }
                    });
        // 如果无更新 删除旧数据
        if(!CollectionUtil.isEmpty(updateList)){
            updateBatchById(updateList);
        }else {
            remove(
                    new QueryWrapper<SysColumnSetting>().lambda()
                            .eq(SysColumnSetting::getUserId,userId)
                            .eq(SysColumnSetting::getPath,addList.get(0).getPath())
            );
        }
        if(!CollectionUtil.isEmpty(addList)){
            saveBatch(addList);
        }
    }
}
